<template>
  <div>
    <el-dialog
      :title="info.isadd ? '规格添加' : '规格编辑'"
      :visible.sync="info.isshow"
      @closed="closed"
    >
      <el-form :model="user"
        >{{ user }}
    
        <el-form-item label="手机号" label-width="120px">
          <el-input v-model="user.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称" label-width="120px">
          <el-input v-model="user.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>

        
        <!-- <el-form-item label="规格属性" label-width="120px">
          <div class="line">
            <el-input v-model="user.catename" autocomplete="off"></el-input>
            <el-button type="danger">删除</el-button>
          </div>
        </el-form-item> -->

        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="user.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary"  v-if="info.isadd"
          >确 定添加</el-button
        >
        <el-button type="primary" @click="updata" v-else>确定修改</el-button>
      
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { IndexRoutes } from "../../../router";
import { reqspecsadd, reqspecsinfo, reqspecsedit, reqvipinfo, reqvipedit } from "../../../http/api";
import { errorAlert, successAlert } from "../../../utils//alert";
import path from "path";
export default {
  props: ["info"],
  data() {
    return {
      // 菜单列表出数值
      menuList: [],

      //   表单数据
      user: {
        nickname: "",
        phone: "",
        password:"",
        status: 2,
      },
      //  规格属性
      attrArr: [
        {
          val: "",
        },
        {
          val: "",
        },
      ],
    };
  },
  computed: {
    ...mapGetters({ list: "vip/list" }),
  },
  methods: {
    ...mapActions({ reqList: "vip/reqList" }),
    cancel() {
      this.info.isshow = false;
    },

    // 清空user
    empty() {
      this.user = {
        uid:"",
        nickname: "",
        phone: "",
        password:"",
        status: 2,
      };

    },
  
  // 检查数据
  checData(){
     return new Promise(resolve=>{
        if(this.user.nickname===""){
        errorAlert("昵称不能为空")
      }
          if(this.user.phone===""){
        errorAlert("手机号不能为空")
      }
    
     
    
      resolve()
     })
  },
    // 添加数据
    // add() {
     
    // this.checData().then(()=>{
    //        // 将attrArr赋值给attr [{val:1},{val:2}] 转成[1,2,3]
    //   this.user.attrs = this.attrArr.map((item) => item.val).join(",");

    //   reqspecsadd(this.user).then((res) => {
    //     if (res.data.code === 200) {
    //       successAlert(res.data.msg);
    //       // 清空·user
    //       console.log(res);
    //       this.empty();
    //       this.info.isshow = false;
    //       // 刷新列表
    //       this.reqList();
    //     }
    //   });
    // })
    // },
    // changpia() {
    //   if (this.user.pid ===0) {
    //     this.user.type = 1;
    //   } else {
    //     this.user.type = 2;
    //   }
    // },
    getone(uid) {
      reqvipinfo({ uid: uid }).then((res) => {
        console.log(res.data.code === 200);
        if (res.data.code == 200) {
          // console.log(res);
          this.user = res.data.list;
           // 补个id
          // this.user.id=id;
             this.user.password=""
          
      
  
        }
      });
    },
    // 修改
    updata() {
      this.checData().then(()=>{
     
      reqvipedit(this.user).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);

          this.empty();
          this.info.isshow = false;
          // 刷新列表
          this.reqList();
        }
      });
      })
    
    },
    closed() {
      // 若果是编辑的关闭 就执行empty
      // 不是添加
      if (!this.info.isadd) {
        this.empty();
      }
    },
  },
  mounted() {
    // 请求菜单列表数据
    // reqmenulist({ istree: true }).then((res) => {
    //   if (res.data.code == 200) {
    //     console.log(res);
    //     this.menuList = res.data.list;
    //   }
    // });
  },
};
</script>
<style lang="less" scoped>
@import "../../../less/index.less";
.line {
  display: flex;
  .el-input {
    flex: 1;
  }
}
</style>